unit Visualisator3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

const
  n=15;
  a=10;
  r=100;
  b=5;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Panel1: TPanel;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  p:array [0..n] of TPanel;
  tic, tac,d:integer;


implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
  timer1.Enabled:=false;
  for i:=0 to n-1 do
  begin
    p[i]:=TPanel.Create(Form1);
    p[i].Parent:=Form1;
    p[i].Top:=a;
    p[i].Left:=a*i+a+b*i;
    p[i].Height:=a;
    p[i].Width:=a;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
  for i:=0 to n-1 do
    p[i].Caption:=IntToStr(random(r));
  Timer1.Enabled:=true;
  tic:=0;
  tac:=0;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var t:string;
begin
  if StrToInt(p[tic].Caption)>StrToInt(p[tic+1].Caption) then
   begin
    p[tic].Left:=p[tic].Left+1;
    p[tic+1].Left:=p[tic+1].Left-1;
  end;
  if p[tic].Left=p[tic+1].Left then
  begin
    t:=p[tic].Caption;
    p[tic].Caption:=p[tic+1].Caption;
    p[tic+1].Caption:=t;
  end;
  if (StrToInt(p[tic].Caption)<=StrToInt(p[tic+1].Caption)) and (p[tic].Left=a*tic+a+b*tic) then
  begin
    tic:=tic+1;
    if tic= n-2-tac then
    begin
      tic:=0;
      tac:=tac+1;
    end;
    if tac=n-2 then
      Timer1.Enabled:=false;
  end;
  if (StrToInt(p[tic].Caption)<=StrToInt(p[tic+1].Caption)) and (p[tic].Left<>a*tic+a+b*tic) then
  begin
    p[tic].Left:=p[tic].Left-1;
    p[tic+1].Left:=p[tic+1].Left+1;
  end;
end;

end.
